package com.maserb.monitor.repository.schoolStudent;

import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentParentEntity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SchoolStudentRepository extends JpaSpecificationExecutor<SchoolStudentEntity>
        , PagingAndSortingRepository<SchoolStudentEntity, String> {

    Integer countByOrgId(String orgId);

    List<SchoolStudentEntity> findAllByOrgIdOrderByNameDesc(String orgId);

    @Query(value = "select r from SchoolStudentEntity r  where r.orgId=?1 and (r.name =?2 or r.studentId=?2)")
    List<SchoolStudentEntity> findAllByNameOrId(String orgId,String nameOrId);

    SchoolStudentEntity findFirstByCardNumber(String cardNumber);

    SchoolStudentEntity findFirstByStudentId(String studentId);

    List<SchoolStudentEntity> findByParent (SchoolStudentParentEntity parent);

    @Query(value = "select j from SchoolStudentEntity r left join JwOrgEntity j on r.orgId = j.id where r.parent=?1  group by j")
    List<JwOrgEntity> getOrgListByParent (SchoolStudentParentEntity parent);

    SchoolStudentEntity findFirstByCid(Long cid);

    List<SchoolStudentEntity> findByName(String name);

    List<SchoolStudentEntity> findByOrgNameLike(String orgName);

    SchoolStudentEntity findFirstBySchoolIdAndStudentId(String schoolId, String studentId);

    List<SchoolStudentEntity> findAllByParentIsNull();
}